<template>
  <el-dialog :model-value="dialogVisible" :title="state.title" width="55%" align-center @closed="handleCancel">
    <el-form
      ref="refForm"
      :model="form.data"
      :rules="form.rules"
      label-position="right"
      :label-width="130"
      style="padding-right: 25px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item prop="jobName" label="任务名称">
            <el-input v-model="form.data.jobName" placeholder="请输入任务名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="jobGroup" label="任务组名">
            <el-input v-model="form.data.jobGroup" disabled placeholder="请输入任务组名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="invokeTarget" label="调用目标方法">
            <el-input v-model="form.data.invokeTarget" placeholder="请输入调用目标方法"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="cronExpression" label="cron执行表达式">
            <el-input v-model="form.data.cronExpression" placeholder="请输入cron执行表达式"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="misfirePolicy" label="执行错误策略">
            <el-radio-group v-model="form.data.misfirePolicy" size="small">
              <el-radio-button type="warning" :label="1">立即执行</el-radio-button>
              <el-radio-button :label="2">执行一次</el-radio-button>
              <el-radio-button :label="3">放弃执行</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="concurrent" label="是否并发执行">
            <el-radio-group v-model="form.data.concurrent">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="status" label="状态">
            <el-radio-group v-model="form.data.status">
              <el-radio :label="1">运行</el-radio>
              <el-radio :label="0">暂停</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item prop="remark" label="备注信息">
        <el-input v-model="form.data.remark" type="textarea" placeholder="请输入备注信息"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave">保 存</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { withDefaults, computed, reactive, ref, nextTick } from 'vue'
import { ElDialog, ElMessage, ElForm, ElFormItem, ElInput, ElRadioGroup, ElRadio, ElRadioButton } from 'element-plus'
import { util } from '@/utils'
import { jobApi } from '@/api/modules/job/job'
import type { Job } from '@/api/modules/job/job'

const props = withDefaults(defineProps<{ visible: boolean }>(), {
  visible: false
})
const emits = defineEmits(['success', 'update:visible'])

const dialogVisible = computed(() => props.visible)

const refForm = ref()
const form = reactive<{ data: Job; rules: any }>({
  data: { misfirePolicy: '3', concurrent: 0, status: 1, jobGroup: 'DEFAULT' } as Job,
  rules: {
    jobName: [{ required: true, message: '请输入任务名称' }]
  }
})

const state = computed(() => {
  if (form.data.jobId) {
    return { isCreate: false, title: '修改定时任务' }
  } else {
    return { isCreate: true, title: '新建定时任务' }
  }
})

const handleSave = () => {
  refForm.value.validate((valid: boolean) => {
    if (valid) {
      // log.action(state.value.title)
      if (state.value.isCreate) {
        saveCallback(jobApi.create(form.data))
      } else {
        saveCallback(jobApi.update(form.data))
      }
    }
  })
}
const saveCallback = (promise: Promise<AxiosType.Response>) => {
  promise.then(resp => {
    if (resp.success) {
      // todo 此处需要根据业务实际情况修改提示内容字段jobId, 一般是名字或编号
      ElMessage.success(`已${state.value.title} [ ${form.data.jobId} ]`)
      emits('success')
      handleCancel()
    } else {
      ElMessage.error(resp.message)
    }
  })
}

const handleCancel = () => {
  form.data = { misfirePolicy: '3', concurrent: 0, status: 1, jobGroup: 'DEFAULT' } as Job
  refForm.value.resetFields()
  emits('update:visible', false)
}

const setFormData = (data: Job) => {
  nextTick(() => (form.data = util.cloneObject(data)))
}
defineExpose({ setFormData })
</script>

<style lang="scss" scoped></style>
